package com.gupao.javavip.netty.push.client.processor.handler;

import com.gupao.javavip.netty.push.api.Request;
import com.gupao.javavip.netty.push.client.processor.PushMsgClientService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
public abstract class AbstractPushMsgRequestClientHandler extends AbstractPushMsgClientHandler<Request> {
    protected PushMsgClientService service = new PushMsgClientService();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("成功登录推送服务器");
        whenActive(ctx);
    }

    protected void whenActive(ChannelHandlerContext ctx) {
        sendRequest(ctx);
    }

    protected void sendRequest(ChannelHandlerContext ctx) {
        Request request = buildRequest();
        doSendRequest(ctx, request);
    }

    private void doSendRequest(ChannelHandlerContext channelHandlerContext, Request request) {
        log.info("开始处理消息...");
        channelHandlerContext.channel().writeAndFlush(request).addListener(buildSendListener(request));
    }

    protected abstract GenericFutureListener<Future<? super Void>> buildSendListener(Request request);

    protected abstract Request buildRequest();

    @Override
    protected String buildReadTips() {
        return "发送消息";
    }
}
